import { Injectable } from '@angular/core'
import { HttpClient } from '@angular/common/http'
import { Observable, throwError } from 'rxjs'
import { map, catchError } from 'rxjs/operators'

import { COMB_URL } from 'app/utils/contraints'
import { IManualAuthQuery, IManualAuth, IManualAuthCollection } from '@shared/interfaces/manualAuth'
import { IGenericPagination } from '@shared/interfaces/pagination'
import { preProcessParams, encodeParams } from '@shared/decorators'

@Injectable()
export class ManualAuthService {
  constructor(private http: HttpClient) {}

  @preProcessParams()
  @encodeParams()
  getList(params: IManualAuthQuery, str?: string): Observable<IGenericPagination<IManualAuth>> {
    const url = `${COMB_URL}/camel/settle/authmanuals?${str}`
    return this.http.get<IGenericPagination<IManualAuth>>(url)
  }

  createManualAuth(data: IManualAuthCollection): Observable<boolean> {
    const url = `${COMB_URL}/camel/settle/authmanual`

    return this.http
      .post(url, data, {
        observe: 'response',
      })
      .pipe(
        map(resp => {
          const { status } = resp
          if (status === 200) {
            return true
          }
          throwError(resp)
        }),
        catchError(error => {
          return throwError(error)
        }),
      )
  }

}
